<?php
class Model_PostsMapper
{
    protected $_dbTable;
    
    public function __construct($dbTable=null)
    { 
        if($dbTable === null){
            $this->_dbTable = new Model_DbTable_Post();
        } else {
            $this->_dbTable = $dbTable;
        }
    }
    
    public function save($post)
    {
        $data = array(
            'id'      => $post->id,
            'title'   => $post->title,
            'text'    => $post->text,
            'blog_id' => $post->blog_id,
            'main'    => $post->main
        );
        
        if($post->id === null){
            $data['date'] = new Zend_Db_Expr('NOW()');
            $postId = $this->_dbTable->insert($data);
            $post->id = $postId;
            $syncTable = new Model_DbTable_Sync();
            $syncTable->insert(array('post_id'=>$post->id));
        } else {
            $this->_dbTable->update($data, array('id = ?' => (integer) $post->id));
        }
        return $post;
    }
    
    public function delete($post)
    {
        $this->_dbTable->delete(array('id = ?' => (integer) $post->id));
    }
    
    public function getPostsByBlog($blog)
    {
        $rowset = $this->_dbTable->fetchAll(array('blog_id = ?' => (integer) $blog->id));
        $posts = array();
        foreach($rowset as $row){
            $posts[$row->id] = new Model_Post(
                array( 
                    'id'      => $row->id,
                    'title'   => $row->title,
                    'text'    => $row->text,
                    'date'    => $row->date,
                    'blog_id' => $row->blog_id,
                    'main'    => $row->main
                ));
        }
        return $posts;
    }
    
    public function getPostsByPage($page, $blog=null, $itemCountPerPage = 30)
    {
        $select = $this->_dbTable->select();
        if($blog != null){
            $select->where('blog_id = ?' , (integer) $blog->id);
        }
        $paginator = new Zend_Paginator(new App_Paginator_Adapter_Posts($select));
        $paginator->setItemCountPerPage($itemCountPerPage);
        $paginator->setCurrentPageNumber($page);
        return $paginator;
    }
    
    public function find($id)
    {
        $row = $this->_dbTable->find((integer) $id)->current();
        if($row){
            $data = array(
                'id'      => $row->id,
                'title'   => $row->title,
                'text'    => $row->text,
                'date'    => $row->date,
                'blog_id' => $row->blog_id,
                'main'    => $row->main
            );
            return new Model_Post($data);
        } else {
            return null;
        }
    }
    
    public function getMainPosts()
    {
        $rowset = $this->_dbTable->fetchAll('main=1');
        $posts = array();
        foreach($rowset as $row){
            $posts[$row->id] = new Model_Post(
                array( 
                    'id'      => $row->id,
                    'title'   => $row->title,
                    'text'    => $row->text,
                    'date'    => $row->date,
                    'blog_id' => $row->blog_id,
                    'main'    => $row->main
                ));
        }
        return $posts;
    }
    
    public function getPostByComment($comment)
    {
        return $this->find($comment->post_id);   
    }
}